package com.hb.medical_research_system.controller;

import com.hb.medical_research_system.common.Results;
import com.hb.medical_research_system.service.ArticleService;
import com.hb.medical_research_system.service.FileUploadService;
import com.hb.medical_research_system.service.ResultService;
import com.hb.medical_research_system.service.SearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * 文件上传和下载
 */
@Api(tags = "通用")
@Slf4j
@RestController
@RequestMapping("/common")
public class FileUploadController {

    private final FileUploadService fileUploadService;

    @Autowired
    public FileUploadController(FileUploadService fileUploadService) {
        this.fileUploadService = fileUploadService;
    }

    @Autowired
    private SearchService searchService;

    @Autowired
    private ResultService resultService;

    @Autowired
    private ArticleService articleService;

    @Value("${medical.path}")
    private String BasePath;

    @ApiOperation("上传文件+新增文献")
    @PostMapping("/bingo")
    public Results bingo(MultipartFile file){
        String addr = fileUploadService.upload(file).getMsg();
        articleService.add(addr);
        return null;
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    @ApiOperation("文件上传接口")
    @PostMapping("/upload")
    public Results upload(@ApiParam("上传的文件")MultipartFile file){
        return fileUploadService.upload(file);
    }

    /**
     * 文件下载
     * @param name
     * @param response
     */
    @ApiOperation("用户下载接口")
    @GetMapping("/download")
    public Results download(@ApiParam("下载的文件名")String name, @ApiParam("Http_response") HttpServletResponse response){
        return fileUploadService.download(name, response);
    }

}
